#include<iostream>
using namespace std;
#define   GRIDSIZE            8                //棋盘大小8*8下标0-7
#define   EMPTY               0                //无棋子
#define   WHITE               1                //黑方棋子
#define   BLACK               2                //白方棋子
#define   OBSTACLE            3                //障碍
#define   INF                 999999
typedef struct moveType {
	int x[3];                                  //x[0]起点  x[1]落点  x[2]障碍
	int y[3];                                  //y[0]起点  y[1]落点  y[2]障碍
}moveType;
int currBotColor;                              //我所执子颜色（1为黑，-1为白，棋盘状态亦同）
int gridInfo[GRIDSIZE][GRIDSIZE] = { 0 };      //先x后y，记录棋盘状态
int dx[8] = { -1,-1,-1,0,0,1,1,1 };
int dy[8] = { -1,0,1,-1,1,-1,0,1 };
int posx[8];                                   //4白4黑的位置
int posy[8];
int turnID = 0;

inline bool inMap(int x, int y) {           //判断在地图内
	if (x < 0 || x >= GRIDSIZE || y < 0 || y >= GRIDSIZE)
		return false;
	return true;
}
void getPos() {
	int whiteorder = 0, blackorder = 4;
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 8; j++) {
			if (gridInfo[i][j] == WHITE)
			{
				posx[whiteorder] = i;
				posy[whiteorder++] = j;
			}
			if (gridInfo[i][j] == BLACK)
			{
				posx[blackorder] = i;
				posy[blackorder++] = j;
			}
		}
}
void moveChess(moveType move)
{
	for (int i = 0; i < 8; i++)
		if (posx[i] == move.x[0] && posy[i] == move.y[0])
		{
			posx[i] = move.x[1];
			posy[i] = move.y[1];
		}
	gridInfo[move.x[1]][move.y[1]] = gridInfo[move.x[0]][move.y[0]];
	gridInfo[move.x[0]][move.y[0]] = EMPTY;
	gridInfo[move.x[2]][move.y[2]] = OBSTACLE;
}
void removeChess(moveType move)
{
	for (int i = 0; i < 8; i++)
		if (posx[i] == move.x[1] && posy[i] == move.y[1])
		{
			posx[i] = move.x[0];
			posy[i] = move.y[0];
		}
	gridInfo[move.x[2]][move.y[2]] = EMPTY;
	gridInfo[move.x[0]][move.y[0]] = gridInfo[move.x[1]][move.y[1]];
	gridInfo[move.x[1]][move.y[1]] = EMPTY;
}

void outPutGrid(int grid[][8]) {
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
			if (grid[i][j] < 9)
				cout << " " << grid[i][j];
			else
				cout << " x";
		cout << endl;
	}
	cout << endl;
}

moveType moveNode[1701][4];
int createMove(int color, int depth) {
	int stacklen = 0;
	int i, imax;
	if (color == WHITE) { i = 0; imax = 4; }
	else { i = 4; imax = 8; }
	for (; i < imax; i++) {
		gridInfo[posx[i]][posy[i]] = EMPTY;
		for (int direction = 0; direction < 8; direction++) {
			for (int step = 1; step < 8; step++) {
				int xx = posx[i] + dx[direction] * step;
				int yy = posy[i] + dy[direction] * step;
				if (!inMap(xx, yy))break;
				if (gridInfo[xx][yy] != EMPTY)break;
				for (int _direction = 0; _direction < 8; _direction++) {
					for (int _step = 1; _step < 8; _step++) {
						int xxx = xx + dx[_direction] * _step;
						int yyy = yy + dy[_direction] * _step;
						if (!inMap(xxx, yyy))break;
						if (gridInfo[xxx][yyy] != EMPTY)break;
						moveNode[stacklen][depth].x[0] = posx[i];
						moveNode[stacklen][depth].y[0] = posy[i];
						moveNode[stacklen][depth].x[1] = xx;
						moveNode[stacklen][depth].y[1] = yy;
						moveNode[stacklen][depth].x[2] = xxx;
						moveNode[stacklen++][depth].y[2] = yyy;
					}
				}
			}
		}
		gridInfo[posx[i]][posy[i]] = color;
	}
	return stacklen;
}//在当前地图上的所有走法

void kingMove(int x, int y, int ans, int movemap[][8]) {
	for (int i = 0; i < 8; i++)
	{
		int xx = x + dx[i], yy = y + dy[i];
		if (inMap(xx, yy) && gridInfo[xx][yy] == EMPTY)
			if (movemap[xx][yy] > ans)
			{
				movemap[xx][yy] = ans;
				kingMove(xx, yy, ans + 1, movemap);
			}
	}
}
void queenMove(int x, int y, int ans, int movemap[][8]) {
	for (int i = 0; i < 8; i++)
	{
		for (int j = 1; j < 8; j++)
		{
			int xx = x + j * dx[i], yy = y + j * dy[i];
			if (inMap(xx, yy) && gridInfo[xx][yy] == EMPTY)
			{
				if (movemap[xx][yy] > ans)
				{
					movemap[xx][yy] = ans;
					kingMove(xx, yy, ans + 1, movemap);
				}
			}
			else break;
		}
	}
}
int whiteMinStep[GRIDSIZE][GRIDSIZE];
int blackMinStep[GRIDSIZE][GRIDSIZE];
int evaluationKing(int color) {
	int evaluationKing = 0; int whiteClosed = 0; int blackClosed = 0; int even = -2; if (color == currBotColor)even = 2;
	for (int i = 0; i < 8; i++) {
		for (int j = 0; j < 8; j++) {
			whiteMinStep[i][j] = INF;
			blackMinStep[i][j] = INF;
		}
	}
	for (int i = 0; i < 4; i++)
	{
		kingMove(posx[i], posy[i], 1, whiteMinStep);
		kingMove(posx[i + 4], posy[i + 4], 1, blackMinStep);
	}

	//outPutGrid(whiteMinStep); outPutGrid(blackMinStep);//test

	if (color == WHITE) {
		for (int i = 0; i < GRIDSIZE; i++) {
			for (int j = 0; j < GRIDSIZE; j++) {
				if (gridInfo[i][j] == EMPTY) {
					if (blackMinStep[i][j] > 4)
						blackClosed++;
					if (whiteMinStep[i][j] > 4)
						whiteClosed++;
					if (whiteMinStep[i][j] < blackMinStep[i][j])
					{
						evaluationKing += 7;
						if (whiteMinStep[i][j] + 1 < blackMinStep[i][j])
							evaluationKing += 5;
					}
					if (whiteMinStep[i][j] > blackMinStep[i][j])
					{
						evaluationKing += -7;
						if (whiteMinStep[i][j] > blackMinStep[i][j] + 1)
							evaluationKing += -5;
					}
					if (whiteMinStep[i][j] == blackMinStep[i][j])
						if (whiteMinStep[i][j] <= 3)
							evaluationKing += even;
				}
			}
		}
		evaluationKing += (blackClosed * blackClosed - whiteClosed * whiteClosed) * 3;
	}
	else {
		for (int i = 0; i < GRIDSIZE; i++) {
			for (int j = 0; j < GRIDSIZE; j++) {
				if (gridInfo[i][j] == EMPTY) {
					if (blackMinStep[i][j] > 4)
						blackClosed++;
					if (whiteMinStep[i][j] > 4)
						whiteClosed++;
					if (blackMinStep[i][j] < whiteMinStep[i][j])
					{
						evaluationKing += 7;
						if (blackMinStep[i][j] + 1 < whiteMinStep[i][j])
							evaluationKing += 5;
					}
					if (blackMinStep[i][j] > whiteMinStep[i][j])
					{
						evaluationKing += -7;
						if (blackMinStep[i][j] > whiteMinStep[i][j] + 1)
							evaluationKing += -5;
					}
					if (whiteMinStep[i][j] == blackMinStep[i][j])
						if (blackMinStep[i][j] <= 3)
							evaluationKing += even;
				}
			}
		}
		evaluationKing -= (blackClosed * blackClosed - whiteClosed * whiteClosed) * 3;
	}
	return evaluationKing;
}
int evaluationQueen(int color) {
	int evaluationQueen = 0; int whiteClosed = 0; int blackClosed = 0; int even = -2; if (color == currBotColor)even = 2;
	for (int i = 0; i < 8; i++) {
		for (int j = 0; j < 8; j++) {
			whiteMinStep[i][j] = INF;
			blackMinStep[i][j] = INF;
		}
	}
	for (int i = 0; i < 4; i++)
	{
		queenMove(posx[i], posy[i], 1, whiteMinStep);
		queenMove(posx[i + 4], posy[i + 4], 1, blackMinStep);
	}

	//outPutGrid(whiteMinStep); outPutGrid(blackMinStep);//test

	if (color == WHITE) {
		for (int i = 0; i < GRIDSIZE; i++) {
			for (int j = 0; j < GRIDSIZE; j++) {
				if (gridInfo[i][j] == EMPTY) {
					if (blackMinStep[i][j] > 4)
						blackClosed++;
					if (whiteMinStep[i][j] > 4)
						whiteClosed++;
					if (whiteMinStep[i][j] < blackMinStep[i][j])
					{
						evaluationQueen += 7;
						if (whiteMinStep[i][j] + 1 < blackMinStep[i][j])
							evaluationQueen += 5;
					}
					if (whiteMinStep[i][j] > blackMinStep[i][j])
					{
						evaluationQueen += -7;
						if (whiteMinStep[i][j] > blackMinStep[i][j] + 1)
							evaluationQueen += -5;
					}
					if (whiteMinStep[i][j] == blackMinStep[i][j])
						if (whiteMinStep[i][j] <= 3)
							evaluationQueen += even;
				}
			}
		}
		evaluationQueen += (blackClosed * blackClosed - whiteClosed * whiteClosed) * 3;
	}
	else {
		for (int i = 0; i < GRIDSIZE; i++) {
			for (int j = 0; j < GRIDSIZE; j++) {
				if (gridInfo[i][j] == EMPTY) {
					if (blackMinStep[i][j] > 4)
						blackClosed++;
					if (whiteMinStep[i][j] > 4)
						whiteClosed++;
					if (blackMinStep[i][j] < whiteMinStep[i][j])
					{
						evaluationQueen += 7;
						if (blackMinStep[i][j] + 1 < whiteMinStep[i][j])
							evaluationQueen += 5;
					}
					if (blackMinStep[i][j] > whiteMinStep[i][j])
					{
						evaluationQueen += -7;
						if (blackMinStep[i][j] > whiteMinStep[i][j] + 1)
							evaluationQueen += -5;
					}
					if (whiteMinStep[i][j] == blackMinStep[i][j])
						if (blackMinStep[i][j] <= 3)
							evaluationQueen += even;
				}
			}
		}
		evaluationQueen -= (blackClosed * blackClosed - whiteClosed * whiteClosed) * 3;
	}
	return evaluationQueen;
}
int evaluationSigma(int color, int maxMove) {
	return evaluationKing(color) * 2 + evaluationQueen(color) * 3;
}

int main()
{
	moveType move;
	gridInfo[0][(GRIDSIZE - 1) / 3] = gridInfo[(GRIDSIZE - 1) / 3][0] = gridInfo[GRIDSIZE - 1 - ((GRIDSIZE - 1) / 3)][0] = gridInfo[GRIDSIZE - 1][(GRIDSIZE - 1) / 3] = BLACK; gridInfo[0][GRIDSIZE - 1 - ((GRIDSIZE - 1) / 3)] = gridInfo[(GRIDSIZE - 1) / 3][GRIDSIZE - 1] = gridInfo[GRIDSIZE - 1 - ((GRIDSIZE - 1) / 3)][GRIDSIZE - 1] = gridInfo[GRIDSIZE - 1][GRIDSIZE - 1 - ((GRIDSIZE - 1) / 3)] = WHITE;
	cin >> turnID; currBotColor = WHITE;
	for (int i = 0; i < turnID; i++)
	{
		cin >> move.x[0] >> move.y[0] >> move.x[1] >> move.y[1] >> move.x[2] >> move.y[2];
		if (move.x[0] == -1)
			currBotColor = BLACK;
		else
			moveChess(move);//procStep(move, 3 - currBotColor, false); // 模拟对方落子
		if (i < turnID - 1)
		{
			cin >> move.x[0] >> move.y[0] >> move.x[1] >> move.y[1] >> move.x[2] >> move.y[2];
			if (move.x[0] >= 0)
				moveChess(move);//procStep(move, currBotColor, false); // 模拟己方落子
		}
	}
	getPos();
	//针对吴磊
	if ((turnID == 12 && currBotColor == BLACK))
	{
		if (gridInfo[0][0] == OBSTACLE && gridInfo[0][1] == EMPTY && gridInfo[0][2] == OBSTACLE && gridInfo[5][0] == BLACK && gridInfo[0][5] == EMPTY && gridInfo[1][5] == EMPTY && gridInfo[2][5] == BLACK && gridInfo[2][3] == BLACK && gridInfo[1][4] == EMPTY && gridInfo[1][2] == EMPTY && gridInfo[1][3] == EMPTY && gridInfo[1][6] == WHITE)
			cout << "2 3 0 5 1 5" << endl;
	};
	if ((turnID == 8 && currBotColor == WHITE))
	{
		if (gridInfo[0][0] == EMPTY && gridInfo[1][0] == EMPTY && gridInfo[2][0] == EMPTY && gridInfo[6][5] == WHITE && gridInfo[6][4] == EMPTY && gridInfo[6][3] == EMPTY && gridInfo[6][2] == EMPTY && gridInfo[6][1] == BLACK && gridInfo[6][6] == EMPTY)
			cout << "6 5 6 2 6 6" << endl;
	};

	int maxMove[4];
	maxMove[0] = createMove(currBotColor, 0);
	int evaMax = -INF;
	int bestNode;
	int evaTemp;
	if (maxMove[0] >= 0) {
		for (int i = 0; i < maxMove[0]; i++) {
			moveChess(moveNode[i][0]);
			evaTemp = evaluationSigma(currBotColor, maxMove[0]);
			if (evaTemp > evaMax) {
				evaMax = evaTemp;
				bestNode = i;
			}
			removeChess(moveNode[i][0]);
		}
	}
	cout << moveNode[bestNode][0].x[0] << ' ' << moveNode[bestNode][0].y[0] << ' ' << moveNode[bestNode][0].x[1] << ' ' << moveNode[bestNode][0].y[1] << ' ' << moveNode[bestNode][0].x[2] << ' ' << moveNode[bestNode][0].y[2] << endl;
	return 0;
}